<?
if ($_SESSION['is_login'] != true) {
    reHome($urlHome);
}

if (isset($_SESSION['uid'])) {
    $id = $_SESSION['uid'];
}

$act = $_GET['act'];
if (isset($act)) {
    if ($act == "del_avatar") {
        $db->query("UPDATE " . $prefix . "nguoidung SET u_avatar = '' WHERE u_id = '" . $id . "'");
    }
}

////////SUBMIT FORM
if (isset($_POST['submit'])) {
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";
    $id = $_POST['id'];
    $DuongDanAnh_tmp = $upload->upload_file($PathFile, 'txtAvatar');
    if ($DuongDanAnh_tmp != '') {
        $filetype = $upload->file_type($DuongDanAnh_tmp);
        $upload->crop_image($imgWidth, $imgHeight, $PathFile . $DuongDanAnh_tmp, $filetype, $PathThumbFile . $DuongDanAnh_tmp);
        $DuongDanAnh = "'" . $DuongDanAnh_tmp . "'";
    } else {
        $DuongDanAnh = "`u_avatar`";
    }

    $txtShopName = addslashes($_POST['txtShopName']);
    $txtWebsite = addslashes($_POST['txtWebsite']);
    $txtCompany = addslashes($_POST['txtCompany']);
    $txtAddress = addslashes($_POST['txtAddress']);
    $txtQuocGia = $_POST['txtQuocgia'];
    $txtTinhThanh = $_POST['txtTinhThanh'];
    $txtDay = $_POST['txtDay'];
    $txtMonth = $_POST['txtMonth'];
    $txtYear = $_POST['txtYear'];
    $txtDate = $txtYear . "-" . $txtMonth . "-" . $txtDay;
    $dtcodinh = addslashes($_POST['dtcodinh']);
    $dtdidong = addslashes($_POST['dtdidong']);
    $chat = "";
    for ($i = 0; $i < count($arrChat); $i++) {
        if ($i != count($arrChat) - 1) {
            $chat .= $i . "-" . addslashes($_POST['chat_' . $i]) . ";";
        } else {
            $chat .= $i . "-" . addslashes($_POST['chat_' . $i]);
        }
    }
    if ($_POST['txtLoaiGiayTo'] != "") {
        $loaigiayto = $_POST['LoaiGiayTo'] . ";" . addslashes($_POST['txtLoaiGiayTo']);
    }
    $gender = $_POST['gender'];
    $txtHobby = addslashes($_POST['txtHobby']);
    $txtSignature = addslashes($_POST['txtSignature']);

    //Check uname,u_email
    $update = "UPDATE " . $prefix . "nguoidung SET `u_avatar` = $DuongDanAnh, `u_website` = '" . $txtWebsite . "', `u_congty` = '" . $txtCompany . "', `u_tenshop` = '" . $txtShopName . "', `u_diachi` = '" . $txtAddress . "', `u_quocgia` = '" . $txtQuocGia . "', `u_tinhthanh` = '" . $txtTinhThanh . "', `u_ngaysinh` = '" . $txtDate . "', `u_dienthoaicodinh` = '" . $dtcodinh . "', `u_dienthoaididong` = '" . $dtdidong . "', `u_loaigiayto` = '" . $loaigiayto . "', `u_nickchat` = '" . $chat . "', `u_gioitinh` = '" . $gender . "', `u_gioithieubanthan` = '" . $txtHobby . "', `u_chuky` = '" . $txtSignature . "' WHERE u_id = '" . $id . "'";
    echo $update;
    $db->query($update);
    header("Location: " . $root . "/shop-" . $id . "/" . $_SESSION['uname'] . "");
}


$rs = $db->query("SELECT * FROM " . $prefix . "nguoidung WHERE u_id = '" . $id . "' AND u_banned = 1");
$row = $db->fetch_array($rs);

$u_id = $row[u_id];
$u_capdo = $row[u_capdo];
$u_name = stripslashes($row[u_name]);
$u_email = stripslashes($row[u_email]);
$u_tenshop = stripslashes($row[u_tenshop]);
$u_diachi = stripslashes($row[u_diachi]);
$u_website = stripslashes($row[u_website]);
$u_congty = stripslashes($row[u_congty]);
$u_avatar = $PathThumbFile . $row[u_avatar];
$u_quocgia = $row[u_quocgia];
$day = loadNgayThangNam($row[u_ngaysinh], 0);
$month = loadNgayThangNam($row[u_ngaysinh], 1);
$year = loadNgayThangNam($row[u_ngaysinh], 2);
$dtcodinh = stripslashes($row[u_dienthoaicodinh]);
$dtdidong = stripslashes($row[u_dienthoaididong]);
$u_loaigiayto = split(";", $row[u_loaigiayto]);
$u_nickchat = $row[u_nickchat];
$u_gioithieubanthan = stripslashes($row[u_gioithieubanthan]);
$u_chuky = stripslashes($row[u_chuky]);
$u_gioitinh = $row[u_gioitinh];
$u_tinhthanh = $row[u_tinhthanh];
$u_dienthoaicodinh = stripslashes($row[u_dienthoaicodinh]);
$u_dienthoaididong = stripslashes($row[u_dienthoaididong]);
?>
<div id="linkPath">
    <div class="linkPath">
        <div class="linkPath-text">
            <?
            file_exist('includes/inc_thong_tin_rieng.php');
            include('includes/inc_thong_tin_rieng.php');
            ?>

            <div style="clear: both;"></div>
        </div>
    </div>

</div>
<form action="" name="frmSuaTaiKhoan" id="frmSuaTaiKhoan" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" id="id" value="<?=$id?>"/>
<!-- Main Page-Content { -->
<div id="page-content">

<div id="userEditInfo">
	<div class="userEditInfo-content">
		<div class="left">
			<div id="avatar">
				<div class="picture">
					<img src="<?=$root?>/<?=$u_avatar?>" alt="Avatar" onerror="this.src='<?=$root?>/themes/images/noavatar.jpg';" width="120"/></div>
				<p style="padding-left:60px;"><a href="<?=$root?>/?mod=suataikhoan&id=<?=$id?>&act=del_avatar" style="font-weight:bold; color:#0000ff;">[Xóa Ảnh]</a></p>
			</div>
		</div>
		<div class="right">
			<h3 class="form-header">Cập nhật thông tin</h3>

			<div class="field">
				<div class="col-l">Tên tài khoản</div>
				<div class="col-r"><b><?=$u_name?></b></div>
			</div>
			<div class="field">
				<div class="col-l">Địa chỉ email</div>
				<div class="col-r"><b><?=$u_email?></b></div>
			</div>

			<div class="field">
				<div class="col-l">Mật khẩu</div>
				<div class="col-r">
					<a target="_blank" title="Đổi mật khẩu" href="<?=$root?>/doi-mat-khau/" style="color:#0000ff; font-weight:bold;">Đổi mật khẩu</a>
				</div>
			</div>
			<div class="field">
				<div class="col-l">Avatar</div>
				<input type="file" name="txtAvatar" id="txtAvatar" size="50"/>
			</div>
		
			<div class="field">
				<div class="col-l">Tên shop</div>
				<input id="txtShopName" name="txtShopName" maxlength="100" type="text" value="<?=$u_tenshop?>"/>
			</div>
			<div class="field">
				<div class="col-l">Website</div>
				<input id="txtWebsite" name="txtWebsite" type="text" maxlength="100" value="<?=$u_website?>" style="width:320px;"/>
			</div>
			<div class="field">
				<div class="col-l">Công ty</div>
				<input id="txtCompany" name="txtCompany" type="text" maxlength="100" value="<?=$u_congty?>" style="width:320px;"/>
			</div>
			<div class="field">
				<div class="col-l">Địa chỉ</div>
				<textarea id="txtAddress" name="txtAddress" rows="5" cols="37"><?=$u_diachi?></textarea>
			</div>
			<div class="field">
				<span class="col-l">Quốc gia</span>
				<span class="col-r">
				<select id="txtQuocgia" name="txtQuocgia" style="width:100px;"
						onchange="ajaxSelect2Select('txtQuocgia','txtTinhThanh','<?=$root?>/loadTinhThanh.php','IdQuocGia');">
					<option value="-1">---Chọn---</option>
					<?
					$rs = $db->query("SELECT * FROM " . $prefix . "quocgia order by qg_id ASC");
					while ($row = $db->fetch_array($rs)) {
						?>
						<option value="<?=$row[qg_id]?>"><?=$row[qg_ten]?></option>
						<? } ?>
				</select>
				</span>
				<script language="javascript">
					var a = document.getElementById('txtQuocgia');
					for (var i = 0; i < a.length; i++) {
						if (a[i].value == "<?=$u_quocgia?>") {
							a.selectedIndex = i;
							break;
						}
					}
				</script>
				<span class="col-l">Tỉnh/TP</span>
				<span class="col-r">
				<select id="txtTinhThanh" name="txtTinhThanh">
					<option value="-1">---Chọn---</option>

					<?
					if ($IdQuocGia == "") {
						$IdQuocGia = $u_quocgia;
					}
					if ($IdQuocGia) {

						$sql = "select * from " . $prefix . "tinhthanh where tt_maquocgia = '" . $IdQuocGia . "' order by tt_id ASC";
						$rs = $db->query($sql);

						if ($db->num_rows($rs)) {

							while ($rows = $db->fetch_array($rs))

							{

								?>

								<option value="<?=$rows["tt_id"]?>"><?=$rows["tt_ten"]?></option>

								<?

							}

						}

					}

					?>

				</select>
				</span>
				<script language="javascript">
					var a = document.getElementById('txtTinhThanh');
					for (var i = 0; i < a.length; i++) {
						if (a[i].value == "<?=$u_tinhthanh?>") {
							a.selectedIndex = i;
							break;
						}
					}
				</script>
			</div>

			<div class="field">
				<div class="col-l">Ngày sinh</div>
				<select id="txtDay" name="txtDay" style="width:80px;">
					<option value="0" title="Ngày">Ngày</option>
					<?
					for ($i = 1; $i <= 31; $i++) {
						?>
						<option value="<?=$i?>" <? if ($day == $i) { ?> selected="selected" <? }?>><?=$i?></option>
						<? } ?>
				</select>

				<select id="txtMonth" name="txtMonth" style="width:80px;">
					<option value="0" title="Tháng">Tháng</option>
					<?
					for ($i = 1; $i <= 12; $i++) {
						?>
						<option value="<?=$i?>" <? if ($month == $i) { ?> selected="selected" <? }?>><?=$i?></option>
						<? } ?>
				</select>

				<select id="txtYear" name="txtYear" style="width:80px;">
					<option value="0" title="Năm">Năm</option>
					<?
					for ($i = 2005; $i >= 1951; $i--) {
						?>
						<option value="<?=$i?>" <? if ($year == $i) { ?> selected="selected" <? }?>><?=$i?></option>
						<? } ?>
				</select>
			</div>

			<div class="field">
				<div class="field-name">Điện thoại</div>
				<div id="phoneNumber" class="phoneNumber">
					<ul id="phoneNumber-input" class="phoneNumber-input">
						<li>
							<div class="col-l">Cố định</div>
							<span class="col-r">
								<input id="dtcodinh" name="dtcodinh" value="<?=$dtcodinh?>" type="text" />
							</span>
						</li>
						<li>
							<div class="col-l">Di động</div>
							<span class="col-r">
								<input id="dtdidong" name="dtdidong" value="<?=$dtdidong?>" type="text" />
							</span>
						</li>
					</ul>
				</div>
			</div>


			<div class="field">
				<div class="field-name">Nick Chat</div>

				<div id="im" class="im">
					<ul id="keypair-input" class="im-input">
						<?
						for ($i = 0; $i < count($arrChat); $i++) {

							$DBarrChat = split(";", $u_nickchat);
							$str = "";
							for ($j = 0; $j < count($DBarrChat); $j++) {
								$pmchat = split("-", $DBarrChat[$j]);
								//echo $pmchat[0];
								if ($pmchat[0] == $i) {
									$str = $pmchat[1];
								}
							}
							?>
										<li>
											<div class="col-l"><?=$arrChat[$i]?></div>
											<div class="col-r">
												<input id="chat_<?=$i?>" name="chat_<?=$i?>" value="<?=$str?>" type="text" />
											</div>
										<div style="clear:both;"></div>
							<? } ?>
					</li>
					</ul>
				</div>
			</div>

			<div class="field">
				<div class="col-l">Chọn loại giấy tờ</div>
				<select id="LoaiGiayTo" name="LoaiGiayTo" style="width:155px;">
					<?
					for ($i = 0; $i < count($arrLoaiGiayTo); $i++) {
						?>
						<option value="<?=$i?>" <? if ($u_loaigiayto[0] == $i) { ?>
								selected="selected" <? }?>><?=$arrLoaiGiayTo[$i]?></option>

						<? } ?>
				</select>

				<input id="txtLoaiGiayTo" name="txtLoaiGiayTo" type="text" value="<?=$u_loaigiayto[1]?>" maxlength="30"/>
			</div>


			<div class="field">
				<div class="col-l">Giới tính</div>
				<input id="male" name="gender" class="radio" value="1" type="radio"  <? if ($u_gioitinh == 1) { ?>
					   checked="checked" <? } ?>/> Nam
				<input id="female" name="gender" class="radio" value="0" type="radio" <? if ($u_gioitinh == 0) { ?>
					   checked="checked" <? } ?> /> Nữ
			</div>

			<div class="field">
				<div ><div class="col-l">Giới thiệu bản thân</div></div>
				<div style="margin: 0px auto;">
					<textarea id="elm1" name="txtHobby" style="width: 100%; height:350px;"><?=$u_gioithieubanthan?></textarea>
				</div>

			</div>

			<div class="field">
				<div class="col-l">&nbsp;</div>

				<p style="color: gray; font-size:8pt;">Hỗ trợ BB Code.</p>
			</div>

			<div class="field">
				<div><div class="col-l">Chữ ký</div></div>
				<div>
					<textarea id="elm2" name="txtSignature" rows="5" cols="37" style="width:80%; height:350px;"><?=$u_chuky?></textarea>
				</div>
			</div>

			<div class="field">
				<div class="col-l">&nbsp;</div>

				<p style="color: gray; font-size:8pt;">Hỗ trợ BB Code.</p>

			</div>

			<div class="field" style="float:right;">			
				<input id="accept" name="accept" type="submit" value ="Cập nhật" style="height: 30px;width: 100px;background: #f6f7fc;border: 1px solid #d8dfea;margin:10px 10px 10px; font-weight:bold;" />
			</div>
		</div>
	</div>
</div>
	<div style="clear:both;"></div>
	<!-- } Main Page-Content -->
</div>
<input type="hidden" name="submit" id="submit" value="submit"/>
</form>